package com.example.pei.papa.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.bmob.sms.BmobSMS;
import cn.bmob.sms.listener.RequestSMSCodeListener;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.CountListener;
import com.example.pei.papa.R;
import com.example.pei.papa.tables.UserInfo;

public class register_step_one_Activity extends AppCompatActivity implements TextWatcher,View.OnClickListener {

    private EditText editText_phone;
    private Button button_back_to_menu;
    private Button button_nextstep_two;

    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_step_one_);


        ViewInit();

    }

    //重写EditText监听
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {    }
    @Override
    public void afterTextChanged(Editable s) {
        phone=editText_phone.getText().toString();
        if(isMobileNO(phone)){
            button_nextstep_two.setBackgroundColor(Color.parseColor("#12B7F5"));
            button_nextstep_two.setClickable(true);
        }else{
            button_nextstep_two.setBackgroundColor(Color.parseColor("#e9ebec"));
            button_nextstep_two.setClickable(false);
        }
    }

    //重写Button监听
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back_to_menu:finish();break;
            case R.id.btn_to_step_two:btn_nextstep_two_Click();break;
        }
    }

    private void ViewInit(){
        editText_phone=(EditText)findViewById(R.id.et_username);
        button_back_to_menu=(Button)findViewById(R.id.btn_back_to_menu);
        button_nextstep_two=(Button)findViewById(R.id.btn_to_step_two);
        //添加监听
        editText_phone.addTextChangedListener(this);
        button_back_to_menu.setOnClickListener(this);
        button_nextstep_two.setOnClickListener(this);

        button_nextstep_two.setClickable(false);
    }

    private boolean isMobileNO(String phone) {
        String telRegex = "^1[0-9]{10}$";
        if (TextUtils.isEmpty(phone)) return false;
        else return phone.matches(telRegex);
    }

    private void btn_nextstep_two_Click(){
        BmobQuery<UserInfo> bmobQuery = new BmobQuery<UserInfo>();
        bmobQuery.addWhereEqualTo("mobilePhoneNumber",phone);
        bmobQuery.count(UserInfo.class, new CountListener() {
            @Override
            public void done(Integer count, BmobException e) {
                if(e==null){
                    if(count>0) {
                        Toast.makeText(register_step_one_Activity.this,"该手机号已经被注册",Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent =new Intent("register_step_2_Activity");
                        intent.putExtra("phoneString",phone);
                        startActivity(intent);
                        //如果手机号没有被注册，可以发送验证码
                        BmobSMS.requestSMSCode(register_step_one_Activity.this, phone, "交友神器",new RequestSMSCodeListener() {
                            @Override
                            public void done(Integer smsId, cn.bmob.sms.exception.BmobException e) {
                                if (e == null) {
                                    //验证码发送成功
                                }
                                else{
                                    Toast.makeText(register_step_one_Activity.this,"发送失败 Code:"+e.getMessage(),Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }
                }else{
                    Toast.makeText(register_step_one_Activity.this,"请稍后再试,或者检查网络连接", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}
